package algorithm.sorting;

import java.util.Arrays;

//冒泡排序
public class BubbleSort {
    public static void main(String[] args) {
        int[] table = {32,26,87,72,26,17};
        bubbleSort(table);
    }
    public static void bubbleSort(int[] table){
        boolean exchange = true; //是否交换标识
        //有交换时在进行下一趟，最多n-1趟
        for(int i = 1;i<table.length&& exchange;i++){
            exchange = false; //假设为交换
            for(int j =0;j<table.length-1;j++){//一次比较交换
                if(table[j] > table[j+1]) {
                    int temp = table[j];
                    table[j] = table[j+1];
                    table[j+1] = temp;
                    exchange = true; //有交换
                }
            }
            System.out.println(i+"次:");
            System.out.println(Arrays.toString(table));
        }
        System.out.println("结果:");
        System.out.println(Arrays.toString(table));
    }
}
